﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using NetCoreProject.Api.Common;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace NetCoreProject.Api.Filters
{
    public class ModelValidationAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext actionContext)
        {
            var modelState = actionContext.ModelState;
            if (!modelState.IsValid)
            {
                ResponseParamter result = new ResponseParamter();
                result.Code = -1;
                result.Msg = ValidationHelper.TranslateError(modelState);
                actionContext.Result = new JsonResult(result);
            }
        }
    }
}
